<template>
	<view>
		<view class="pdl ffffff flexbetween mainpadding_top">
			<view class="titletext">呼叫訂單</view>
			<u-switch inactiveColor="rgb(230, 230, 230)" v-model="is_call" activeColor="#feb800"></u-switch>
		</view>
		<view class="pdl ffffff flexbetween mainpadding" v-if="hy_type!=5">
			<view class="titletext">使用預約</view>
			<u-switch inactiveColor="rgb(230, 230, 230)" v-model="is_pre" activeColor="#feb800"></u-switch>
		</view>
		<template v-if="is_pre && hy_type!=5">
			<view class="pdl ffffff flexbetween mainpadding">
				<view class="titletext">添加預約時間</view>
				<view class="addimg" @click="messageTime.timeShow=true;"></view>
			</view>
			<view class="flexleft mainpadding2 ffffff flex_wrap">
				<view class="itemtimebox flexcenter margin_top2" v-for="(value,i) in timeArr" :key="i">
					<view class="itemtime">
						<view class="">{{value}}</view>
						<view class="posidel" @click="removeTime(index,i)">
							<u-icon name="close" color="#fff" size="8"></u-icon>
						</view>
					</view>
				</view>
			</view>
		</template>
		<view class="ffffff flexbetween mainpadding">
			<view class="titletext">掃碼開關</view>
			<u-switch inactiveColor="rgb(230, 230, 230)" v-model="is_scan" activeColor="#feb800"></u-switch>
		</view>
		<view class="mainpadding posibottom" style="bottom: 0;">
			<view class="submitbtn" @click="submit">確認</view>
		</view>
		<timePicker :messageTime="messageTime" @timeConfirm="timeConfirm"></timePicker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				shop_id: uni.getStorageSync("shop_id"),
				is_call: false, //是否呼叫订單:0=否,1=是
				is_pre: false, //是否使用预约:0=否,1=是
				is_scan: false, //是否掃碼开关:0=否,1=是
				make_starttime: "",
				make_endtime: "",
				messageTime: {
					timeShow: false,
					title: "選擇時間",
					mode: "time", //date日期，datetime年月日时分秒，time時間，year-month年月
				},
				timevalue: 0, //0開始，1结束
				timeArr: [],
				show:false,
				hy_type: "", //0買賣,1計時，3住宿露营，5工程，6超商
			}
		},
		onLoad() {
			this.hy_type = uni.getStorageSync('hy_type')
			this.getInfo()
		},
		methods: {
			timeConfirm(e) {
				this.timeArr.push(e)
				this.timeArr.sort((a, b) => {
					const [aHours, aMinutes] = a.split(':').map(Number);
					const [bHours, bMinutes] = b.split(':').map(Number);
					// 比較小時
					if (aHours !== bHours) {
						return aHours - bHours;
					}
					// 比較分鐘
					return aMinutes - bMinutes;
				});
			},
			removeTime(index, i) {
				this.timeArr.splice(i, 1)
			},
			async getInfo() {
				let res = await this.http.request("/api/shop/Shopdeail", "POST", {
					shop_id: uni.getStorageSync("shop_id"),
				})
				if (res.code == 1) {
					uni.setStorageSync("appkey", res.data.appkey)
					uni.setStorageSync("is_sdkfp", res.data.is_sdkfp)
					this.is_call = res.data.is_call == 0 ? false : true
					this.is_pre = res.data.is_pre == 0 ? false : true
					this.is_scan = res.data.is_scan == 0 ? false : true
					this.timeArr = res.data.yytime ? res.data.yytime.split(',') : []
				} else if (res.code == 0) {
					this.http.toast(res.msg);
				} else {
					this.http.toast('系統處理中，請稍後再試');
				}
			},
			async submit() {
				let res = await this.http.request("/api/shop/Shopedit", 'POST', {
					shop_id: this.shop_id,
					is_call: this.is_call ? 1 : 0, //是否呼叫订單:0=否,1=是
					is_pre: this.is_pre ? 1 : 0, //是否使用预约:0=否,1=是
					is_scan: this.is_scan ? 1 : 0, //是否掃碼开关:0=否,1=是
					// make_starttime:this.make_starttime,
					// make_endtime:this.make_endtime,
					yytime: this.timeArr.join()
				})
				if (res.code == 1) {
					this.http.toast("提交成功")
					setTimeout(() => {
						uni.navigateBack(1)
					}, 500)
				} else if (res.code == 0) {
					this.http.toast(res.msg)
				} else {
					this.http.toast('系統處理中，請稍後再試');
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.itemtimebox {
		width: 25%;
	}

	.itemtime {
		padding: 30rpx 20rpx;
		width: 150rpx;
		box-sizing: border-box;
		border-radius: 10rpx;
		position: relative;
		background-color: #ffefe7;
		color: #fa5f00;
		font-size: 24rpx
	}

	.posidel {
		position: absolute;
		top: 0;
		right: 0;
		padding: 10rpx 12rpx;
		background-color: #fa5f00;
		border-radius: 0 6px 0 10rpx;
	}
</style>